<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryName" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" />
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" icon="el-icon-plus" @click="handleAddGood">增加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsData" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="50px" align="center" />
        <el-table-column prop="name" label="商品名称" align="center" />
        <el-table-column prop="price" label="商品价格(元)" width="150px" align="center" />
        <el-table-column prop="number" label="商品库存" width="150px" align="center" />
        <el-table-column label="操作" width="200px" align="center">
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { goodsList, delGoods } from '@/api/goods'
export default {
  name: 'GoodsList',
  data() {
    return {
      goodsData: [],
      queryName: ''
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const res = await goodsList(this.queryName)
      if (res.status !== 200) return this.$methods.error('获取商品列表失败！')
      this.goodsData = res.data
    },
    // 商品编辑操作，打开商品弹框
    handleEdit(row) {},
    // 商品删除操作
    async handleDelete(row) {
      const res = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((e) => e)
      if (res === 'cancel') return this.$message.info('已取消删除操作！')
      const result = await delGoods(row.id)
      if (result.status !== 200) return this.$message.error('删除商品失败！')
      this.$message.success('删除商品成功！')
      this.getGoodsList()
    },
    // 增加商品跳转
    handleAddGood() {
      this.$router.push('/addGood')
    }
  }
}
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}
</style>
